package ecnu.ecgbackend.controller;

import ecnu.ecgbackend.entity.User;
import ecnu.ecgbackend.service.*;
import ecnu.ecgbackend.utils.Result;
import ecnu.ecgbackend.vo.RegisterVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class DiagnosisAccountController {
    @Resource
    private DiagnosisAccountService diagnosisAccountService;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        Map<String,Object> result= diagnosisAccountService.login(user);
        if(result==null || result.isEmpty()){
            return Result.error().message("用户名或密码错误");
        }
        User _user=(User) result.get("user");
        if(_user.getIsDelete()!=null && _user.getIsDelete()==1){
            return Result.error().message("该用户已注销");
        }
        return Result.ok(result).message("登录成功！");
    }
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response){
        if(diagnosisAccountService.logout(request,response)){
            return Result.ok().message("退出登录成功！");
        }
        return Result.error().message("退出登录失败");
    }
    @PostMapping("/logoff")
    public Result logoff(){
        if(diagnosisAccountService.logoff()){
            return Result.ok().message("注销成功！");
        }else {
            return Result.error().message("注销失败");
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        return diagnosisAccountService.register(registerVo.getUser(),registerVo.getCode());
    }
    @PostMapping("/password")
    public Result updatePwd(String username,String password){
        if(diagnosisAccountService.updatePassword(username,password)){
            return Result.ok().message("密码修改成功！");
        }
        return Result.error().message("密码修改失败");
    }



}
